﻿using Microsoft.Xna.Framework;
using ArtificialIntelligence.MovementComponent.AStar;
using XNA_2DFramwork;

namespace ArtificialIntelligence.CombatComponent
{
    public class Schedule
    {
        public int Priority;
        public int Weigth;
        public ScheduleType scheduleType;
        public ScheduleState ScheduleState;

        public Schedule(ScheduleType scheduleType)
        {
            ScheduleState = CombatComponent.ScheduleState.Undertermined;
            scheduleType = scheduleType;
        }


        public ScheduleState Run(ScheduleType schedule, Agent agent)
        {
            Vector2 nearestObject = new Vector2();
            Node<Vector2> nodeofNearestOnject = new Node<Vector2>();
            switch (schedule)
	            {
                case ScheduleType.GetWeapon:

	                    ScheduleState = ScheduleState.Running;
	                   nearestObject = agent.FindObject(VisualObject._lVisualObject, ObjectType.Weapon,agent.Position);
	                     nodeofNearestOnject = Path.GetNodeOfVector2(nearestObject);

                        if (nearestObject.X == 0 && nearestObject.Y == 0)
                        {
                            ScheduleState = ScheduleState.Failed;
                        }
                        else
                        {
                            Path.FindPathByAStarSearch(Path.GetListNodeOfVector2(InferenceMachine._domain),
                                                       Path.GetNodeOfVector2(agent.Position),
                                                       nodeofNearestOnject
                                                       , agent);
                            agent.Move(agent.LPath);
                            return ScheduleState = ScheduleState.Excuted;
                        }
	                    break;
 
                case ScheduleType.AttackEnemy:

                       Path.FindPathByAStarSearch(
                           Path.GetListNodeOfVector2(InferenceMachine._domain),
                           Path.GetNodeOfVector2(agent.Position),
                           Path.GetNodeOfVector2(
                                                 agent.FindObject(VisualObject._lVisualObject,
                                                 ObjectType.Enemy ,agent.Position)
                                                 ),
                           agent
                           );

                        agent.AttackObject();
	                    ScheduleState = ScheduleState.Excuted;
                        #region agentTask
                        //AgentTask.FindFlankPosition();
                        //AgentTask.FindPathTo();
                        //AgentTask.AttackObject();)
                        #endregion
                        
                        break;
                
                case ScheduleType.HideFromEnemy:
                     //AgentTask.FindObjectToHideBehind();
                     //AgentTask.FindPathTo();
	                     ScheduleState = ScheduleState.Undertermined;
                     break;
                case ScheduleType.GetObject:

                     ScheduleState = ScheduleState.Running;
                     nearestObject = agent.FindObject(VisualObject._lVisualObject, ObjectType.Bullets, agent.Position);
                     nodeofNearestOnject = Path.GetNodeOfVector2(nearestObject);

                     if (nearestObject.X == 0 && nearestObject.Y == 0)
                     {
                         ScheduleState = ScheduleState.Failed;
                     }
                     else
                     {
                         Path.FindPathByAStarSearch(Path.GetListNodeOfVector2(InferenceMachine._domain),
                                                    Path.GetNodeOfVector2(agent.Position),
                                                    nodeofNearestOnject
                                                    , agent);
                         agent.Move(agent.LPath);
                         return ScheduleState = ScheduleState.Excuted;
                     }
                     break;
	            }

            return ScheduleState;
        }

        public void IncreaseWeight()
        {
            Weigth += 1;
        }

        public void DecreaseWeight()
        {
            Weigth -= 1;
        }

    }
}
